package com.zy.is.common.shiro;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.MD5;
import com.alibaba.druid.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;

/**
 * @ClassName: MyShiroMatcher
 * @Description: 自定义密码验证器
 * @Author: Yanzy
 * @Data: 2021/4/2 15:53
 * @Source: 1.0
 */
@Slf4j
public class MyShiroMatcher extends SimpleCredentialsMatcher{

    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token;
        // 登录密码加密
        String loginPwd = SecureUtil.md5(new String(usernamePasswordToken.getPassword()));
        log.debug("认证加密值: {}", loginPwd);
        // 数据库密码
        String dbPwd = (String) info.getCredentials();
        // 密码比对
        return StringUtils.equalsIgnoreCase(loginPwd, dbPwd);
    }
}
